# -*- coding: utf-8 -*-
"""
@Time    : 2025/2/12 16:29 
@Author  : ZhangShenao 
@File    : 1.stream_reply.py
@Desc    : 流式响应
"""
import os

import dotenv
from openai import OpenAI

# 加载环境变量
dotenv.load_dotenv()

# 创建OpenAI客户端
client = OpenAI(base_url=os.getenv("OPENAI_API_BASE"))

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "帮我解释一下量子力学"}
    ],
    stream=True,  # 采用流式响应
)

# 逐个token打印流式消息
for chunk in resp:
    print(chunk.choices[0].delta.content, flush=True, end="")

print()
